// pages/mine/update/update.js
var app = getApp()
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2018-12-25',
    index: 0,
    picker: ['通知', '志愿', '文体', "竞赛", "讲座", "其他"],
    imgList: [],
    url : '',
    title : '',
    content : '' ,
  },
  formSubmit: function(e) {
    var that = this
    console.log(e.detail.value)
    if (e.detail.value.title.length == 0) {
      wx.showModal({
        content: '标题不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (e.detail.value.source.length == 0) {
      wx.showModal({
        content: '来源不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (e.detail.value.url.length == 0) {
      wx.showModal({
        content: '链接不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (e.detail.value.content.length == 0) {
      wx.showModal({
        content: '摘要不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (e.detail.value.source.length == 0) {
      wx.showModal({
        content: '来源不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (that.data.imgList.length == 0) {
      wx.showModal({
        content: '图片不得为空！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else {
      wx.uploadFile({
        url: app.globalData.url + "news/",
        filePath: that.data.imgList[0],
        name: 'file',
        formData: {
          title: e.detail.value.title,
          content: e.detail.value.content,
          source: e.detail.value.source,
          url: e.detail.value.url,
          type: Number(that.data.index) + 1,
          end_time: that.data.date + " 23:59:59",
          openid: app.globalData.openid
        },

        success(res) {
          console.log("------上传------")
          console.log(res)
          var data = JSON.parse(res.data)
          if (data.state == 1) {
            wx.showToast({
              title: "上传成功", //这里打印出登录成功
              icon: 'success',
              duration: 1000
            })
            setTimeout(function() {
              wx.reLaunch({
                url: '/pages/news/news',
              })
            }, 1000)
          } else {

            wx.showToast({
              title: "未知错误",
              icon: 'none',
              duration: 1000
            })


          }
        }
      })
    }
  },
  ChooseImage() {
    var that = this
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }

    });
    console.log("------imglist------")
    console.log(that.data.imgList)
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '',
      content: '确定要删除这张照片吗？',
      cancelText: '再看看',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  PickerChange(e) {
    console.log("----PickerChange----")
    console.log(e.detail.value);
    this.setData({
      index: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var TIME = util.formatTime(new Date());
    this.setData({
      date: TIME,
    });
    wx.getClipboardData({
      success: function(res) {
        console.log("------url------")
        console.log(res.data)
        var ur = res.data
        if (ur.indexOf("http") != -1) {
          wx.showModal({
            title: '提示',
            content: '检测到你已经复制网址，是否粘贴？',
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                that.setData({
                  url: ur
                })
                wx.request({
                  url: app.globalData.url + 'news/url', // 仅为示例，并非真实的接口地址
                  data: {
                    url: ur
                  },
                  method: "POST",
                  header: {
                    'content-type': 'application/json' // 默认值
                  },
                  success(res) {
                    console.log(res.data)
                    that.setData(
                      {
                        content: res.data.data.content,
                        title: res.data.data.title
                      }
                    )
                  }
                })

              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})